package com.gin.no_gay_alliance.fragment.user.tab;

import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.field.BlockData;
import com.gin.no_gay_alliance.adpter.BlockDataAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.databinding.FragmentConfigBlockDataBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.InputMethodManagerUtils;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理屏蔽设置
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/12 15:17
 **/
public class ConfigBlockDataFragment extends BaseBindingFragment<FragmentConfigBlockDataBinding> implements Callback<BlockData> {
    private static final String TAG = ConfigBlockDataFragment.class.getSimpleName();
    private BlockDataAdapter userAdapter;
    private BlockDataAdapter keywordAdapter;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        // 用户
        userAdapter = new BlockDataAdapter(requireContext());
        b.rvUser.setAdapter(userAdapter);
        // 关键字
        keywordAdapter = new BlockDataAdapter(requireContext());
        b.rvKeyword.setAdapter(keywordAdapter);

        ViewUtils.setOnClickListener(this, b.btSave, b.ivAddKeyword, b.ivAddUser);

        b.swipeRefreshLayout.setOnRefreshListener(this::refresh);
    }

    /**
     * 点击事件统一处理
     *
     * @param v 被点击的View
     */
    @Override
    public void onClick(View v) {
        // 保存屏蔽设置
        if (v == b.btSave) {
            BlockData blockData = new BlockData();
            blockData.setUsers(userAdapter.getData());
            blockData.setKeywords(keywordAdapter.getData());
            MyApplication.app.managerFactory.getBlockDataManager().blockDataSet(blockData, this);
        }
        // 添加屏蔽用户
        if (v == b.ivAddUser) {
            EditText et = new EditText(requireContext());

            AlertDialogUtils.showConfirm(requireContext(), "添加屏蔽用户", "可以填写用户名或用户id, 多个用空格隔开", et, (d, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    String s = String.valueOf(et.getText());
                    if (!ObjectUtils.isEmpty(s)) {
                        List<String> items = Arrays.stream(s.split(" ")).filter(i -> !ObjectUtils.isEmpty(i)).collect(Collectors.toList());
                        userAdapter.pushData(items);
                    }
                }
            });

            et.requestFocus();
            InputMethodManagerUtils.show(handler, et);
        }
        // 添加屏蔽关键字
        if (v == b.ivAddKeyword) {
            EditText et = new EditText(requireContext());

            AlertDialogUtils.showConfirm(requireContext(), "添加屏蔽关键字", "多个用空格隔开", et, (d, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    String s = String.valueOf(et.getText());
                    if (!ObjectUtils.isEmpty(s)) {
                        List<String> items = Arrays.stream(s.split(" ")).filter(i -> !ObjectUtils.isEmpty(i)).collect(Collectors.toList());
                        keywordAdapter.pushData(items);
                    }
                }
            });

            et.requestFocus();
            InputMethodManagerUtils.show(handler, et);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        sendRequest(false);
    }

    /**
     * 请求成功回调
     *
     * @param data 数据
     */
    @Override
    public void onSuccess(BlockData data) {
        b.layoutContent.setVisibility(View.VISIBLE);
        b.tvWarning.setVisibility(View.GONE);
        // 渲染数据
        ui(() -> {
            // 用户
            userAdapter.updateData(data.getUsers());
            // 关键字
            keywordAdapter.updateData(data.getKeywords());
        });
        onRequestFinally();
    }

    /**
     * 请求之前执行的操作
     */
    @Override
    public void beforeRequest() {
        ui(() -> {
            b.tvWarning.setVisibility(View.GONE);
            b.swipeRefreshLayout.setRefreshing(true);
        });
    }

    public void refresh() {
        sendRequest(true);
    }

    /**
     * 显示报错信息，隐藏其他组件
     *
     * @param text 信息
     */
    private void handleException(String text) {
        onRequestFinally();
        ui(() -> {
            b.tvWarning.setText(text);
            b.tvWarning.setVisibility(View.VISIBLE);
            b.layoutContent.setVisibility(View.GONE);
        });
    }

    private void onRequestFinally() {
        ui(() -> {
            b.swipeRefreshLayout.setRefreshing(false);
        });
    }

    /**
     * 执行请求
     *
     * @param ignoreCache 是否忽略缓存
     */
    private void sendRequest(boolean ignoreCache) {
        MyApplication.app.managerFactory.getBlockDataManager().blockDataList(ignoreCache, this);
    }

    public static ConfigBlockDataFragment newInstance() {
        return new ConfigBlockDataFragment();
    }
}
